<?php
require_once(dirname(__FILE__) . "/../../config.php");
ini_set('date.timezone', 'Asia/Shanghai');
error_reporting(E_ERROR);
require_once "../lib/WxPay.Api.php";
require_once "WxPay.NativePay.php";
require_once 'log.php';
$notify = new NativePay();
//模式二
/**
 * 流程：
 * 1、调用统一下单，取得code_url，生成二维码
 * 2、用户扫描二维码，进行支付
 * 3、支付完成之后，微信服务器会通知支付成功
 * 4、在支付成功通知中需要查单确认是否真正支付成功（见：notify.php）
 */
$input       = new WxPayUnifiedOrder();
$_GET['ddh'] = $ddh = $oid;
if (substr($ddh, 0, 3) == 'KE-')
{
    $arr          = $dsql->GetOne("SELECT * FROM #@__shops_orders WHERE oid = '" . $_GET['ddh'] . "'");
    $trr          = $dsql->GetOne("SELECT * FROM #@__arctype WHERE  id = '" . $arr['pid'] . "'");
    $arr['pname'] = $trr['typename'];
    $arr['money'] = $arr['price'];
    if (!is_array($arr))
    {
        die("课程订单号" . $_GET['ddh'] . "有误，请重新操作！");
    }
    $Goods_tag = '会员充值';
    $jiage     = $arr['price'];
}
else
{
    $arr = $dsql->GetOne("SELECT * FROM #@__member_operation WHERE buyid = '" . $_GET['oid'] . "'");
    if (!is_array($arr))
    {
        die("订单号有误，请重新选择开通！");
    }
}

$input->SetBody($arr['pname']);
$input->SetAttach($arr['pname']);
//$input->SetOut_trade_no(WxPayConfig::MCHID . date("YmdHis"));
$input->SetOut_trade_no($_GET['oid']);
$input->SetTotal_fee($arr['money'] * 100);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("在线订单");
$input->SetNotify_url($cfg_basehost . "/user/weixinpay/example/notify.php");
$input->SetTrade_type("NATIVE");
$input->SetProduct_id($_GET['oid']);
$result = $notify->GetPayUrl($input);
$url2   = $result["code_url"];
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>微信支付</title>
    <script type="text/javascript" src="<?php echo $cfg_basehost; ?>/js/jquery.js"></script>
    <style type="text/css">
        * {
            margin:0;
            padding:0;
        }

        body {
            background:#f5f5f5;
            font-size:14px;
            color:#333;
            font-family:"Microsoft Yahei", "微软雅黑", "MicrosoftJhengHei", "华文细黑", "Hiragino Sans GB", "sans-serif";
        }

        ul, li {
            list-style:none;
        }

        h1, h2, h3, h4, h5, h6 {
            font-size:100%;
            font-weight:normal;
        }

        i, em {
            font-style:normal;
        }

        img {
            border:0;
        }

        a {
            text-decoration:none;
            color:#333;
        }

        a:hover {
            text-decoration:none;
            color:#23b8ff;
        }

        .container {
            max-width:1000px;
            margin:0 auto;
        }

        .clearfix:after {
            content:"\200b";
            display:block;
            height:0;
            clear:both;
        }

        /*通用头部*/
        .hd-main {
            background-color:#fff;
            margin-bottom:20px;
        }

        .ep-logo {
            float:left;
            width:60px;
            height:30px;
            margin-top:15px;
            margin-right:10px;
        }

        .ep-logo img {
            display:block;
            width:100%;
            height:100%;
        }

        .ep-hd-info {
            padding-bottom:20px;
        }

        .ep-order-status {
            float:left;
            padding-left:10px;
            border-left:1px solid #ddd;
            line-height:19px;
            font-size:16px;
            font-weight:700;
            margin-top:20px;
        }

        /*支付成功反馈信息*/
        .ep-wp-hd {
            padding:22px 135px;
            background:#f5fef3;
        }

        .ep-status {
            font-size:18px;
            color:#1e1e1e;
            padding:20px 0;
            zoom:1;
            clear:both;
            border:none;
            overflow:hidden;
            text-align:center;
        }

        .ep-status span {
            float:left;
            line-height:25px;
        }

        .ep-icon-success {
            background-position:0 0;
        }

        .ep-icon-success {
            width:23px;
            height:23px;
        }

        .ep-status-success .mod-it-text {
            font-weight:700;
        }

        .ep-status span {
            float:left;
            line-height:25px;
        }

        /*订单信息*/
        .text-center {
            background-color:#fff;
            padding:40px 135px 40px 135px;
        }
    </style>
</head>
<body>

<!-- 头部 -->
<div class="hd-main">
    <div class="container ep-hd-info clearfix">
        <div class="ep-logo">
            <a href="<?php echo $cfg_basehost; ?>/" title="返回首页"><img src="<?php echo $cfg_basehost; ?>/images/logo.png"></a>
        </div>
        <div class="ep-order-status">订单支付</div>
    </div>
</div>
<!-- 头部 End -->

<!-- 订单信息 -->
<div class="container">
    <div class="ep-wp-hd"><h2 class="ep-status ep-status-success">使用微信扫一扫支付</h2></div>
    <div class="text-center" style="text-align:center;">
        <img alt="微信扫码支付" src="qrcode.php?data=<?php echo urlencode($url2); ?>" style="width:200px;height:200px;"/>
    </div>
</div>
<!-- 订单信息 End -->

<script>
    function weipay() {
        $.post("<?php echo $cfg_basehost; ?>/user/weixinpay/ajax.php", {
            'ddh': '<?php echo $_GET['oid']; ?>',
            a: 'getorder'
        }, function (data) {
            if (data != '') {
                location.replace("<?php echo $cfg_basehost; ?>/user/weixinpay/ok.php?ddh=<?php echo $_GET['oid']; ?>");
            }
        })
    }

    setInterval(weipay, 3000);
</script>
</body>
</html>